<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
      >
        <span slot="status" slot-scope="text, record">
          <a-tag color="blue" v-if="text === '1'">
          等待面试
        </a-tag>
          <a-tag color="blue" v-else-if="text === '2'">
          同意面试
        </a-tag>
          <a-tag color="blue" v-else-if="text === '3'">
          面试结束/等待结果
        </a-tag>
          <a-tag color="green" v-else-if="text === '4'">
          同意入职
        </a-tag>
          <a-tag color="red" v-else>
          拒绝入职
        </a-tag>
        </span>
        <span slot="avatar" slot-scope="text, record">
          <a-avatar :src="record.user.avatar"/>
        </span>
        <span slot="work" slot-scope="text, record">
          <ellipsis :length="20" :tooltip="false">{{ record.work.name }}</ellipsis>
        </span>
        <span slot="username" slot-scope="text, record">
          <ellipsis :length="20" :tooltip="false">{{ record.user.name }}</ellipsis>
        </span>

        <span slot="action" slot-scope="text, record">
          <template v-if="record.status === '1' || !record.status">
            <a @click="handleEdit(record, 2)">同意面试</a>
          </template>
          <template v-else-if="record.status === '2'">
            <a @click="handleEdit(record, 3)">面试结束</a>
          </template>
          <template v-else-if="record.status === '3'">
            <a @click="handleEdit(record, 4)">同意入职</a>
            <a-divider type="vertical"/>
            <a @click="handleEdit(record, 5)">拒绝入职</a>
          </template>
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import {STable, Ellipsis} from '@/components'
import {getList, edit} from '@/api/application'

const columns = [
  {
    title: '头像',
    dataIndex: 'avatar',
    scopedSlots: {customRender: 'avatar'}
  },
  {
    title: '姓名',
    align: 'center',
    dataIndex: 'username',
    scopedSlots: {customRender: 'username'}
  },
  {
    title: '岗位',
    align: 'center',
    dataIndex: 'work',
    scopedSlots: {customRender: 'work'}
  },
  {
    title: '联系电话',
    align: 'center',
    dataIndex: 'phone',
    customRender: (e, record) => {
      return record.user.phone
    }
  },
  {
    title: '应聘状态',
    align: 'center',
    dataIndex: 'status',
    scopedSlots: {customRender: 'status'}
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'created_at',
    customRender: e => {
      return moment(e).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  {
    title: '邀约时间',
    align: 'center',
    dataIndex: 'invite_time',
    customRender: e => {
      return e ? moment(e).format('YYYY-MM-DD HH:mm:ss') : '-'
    }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: {customRender: 'action'}
  }
]

export default {
  name: 'application',
  components: {
    STable,
    Ellipsis,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleEdit(record, status) {
      let params = {}
      status = status.toString();
      if (status === "2") {
        params = {
          status,
          invite_time: moment().format('YYYY-MM-DD HH:mm:ss')
        }
      } 
      else {
        params = {
          status,
        }
      }
      edit(params, record.id).then(ret => {
        this.visible = false
        this.confirmLoading = false
        // 刷新表格
        this.$refs.table.refresh()
        this.$message.info('处理成功')
      })
    },
  }
}
</script>
